package com.xu.demo.feature.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EasyPoiTest {
    public static void main(String[] args) throws IOException {
        List<JSONObject> rowDataList = new ArrayList<>();// Map作为每一行的数据容器，List作为行的容器
        List<ExcelExportEntity> keyList = new ArrayList<>();// 每个ExcelExportEntity存放Map行数据的key

        for (int i = 0; i < 10; i++) {
            ExcelExportEntity excelExportEntity = new ExcelExportEntity(i+" title");// 标题
            excelExportEntity.setKey(String.valueOf(i));// rowdata 和 表头 根据这个一一对应
            excelExportEntity.setWidth(20);// 列宽
            keyList.add(excelExportEntity);
        }

        for (int i = 0; i < 5; i++) {// 行
            JSONObject tmp = new JSONObject();
            for (int j = 0; j < 10; j++) {// 列
                tmp.put(j+"", "asdasdadsa");
            }
            rowDataList.add(tmp);
        }

        ExportParams exportParams = new ExportParams(); // excel总体设置
        exportParams.setSheetName("sheet1"); // 指定sheet名字
        exportParams.setType(ExcelType.XSSF);// 兼容office

        // 生成workbook 并导出
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, keyList, rowDataList);
        workbook.getSheetAt(0).getRow(0).setHeightInPoints(30);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.RED.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 给一个格子背景颜色调成红色
        workbook.getSheetAt(0).getRow(1).getCell(0).setCellStyle(cellStyle);
//        workbook.getSheetAt(0).getRow(1).setHeightInPoints(55);

        // 输出到文件
        FileOutputStream fileOutputStream = new FileOutputStream("D:/test.xlsx");
        //导出完成
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        workbook.close();

        // controller 下载文件流
//        @ResponseBody
//        @RequestMapping("/exportFile")
//        public void exportFile(@RequestBody Map<String, Object> params, HttpServletResponse response){

//        try {
//            ServletOutputStream outputStream = response.getOutputStream();
//            workbook.write(outputStream);
//
//            //响应文件格式
//            response.setContentType("application/octet-stream");
//            response.setHeader("Content-Disposition", "attachment; filename=export.xlsx");
//
//            outputStream.flush(); //刷新
//            outputStream.close();
//            workbook.close();
//        } catch (Exception e) {
//            log.error("exception message", e);
//        }
    }
}
